package com.sunshine.lnuplus.view.Me;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.snackbar.Snackbar;
import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.databinding.FragmentMeBinding;
import com.sunshine.lnuplus.utils.DatabaseHelper;
import com.sunshine.lnuplus.utils.DialogFragmentHelper;
import com.sunshine.lnuplus.utils.TagHelper;
import com.sunshine.lnuplus.view.About.AboutActivity;
import com.sunshine.lnuplus.view.AppSetting.AppSettingActivity;
import com.sunshine.lnuplus.view.Login.LoginActivity;
import com.sunshine.lnuplus.view.Main.MainActivity;
import com.sunshine.lnuplus.view.TimetableSetting.TimetableSettingActivity;
import com.sunshine.lnuplus.view.Welcome.WelcomeActivity;

public class MeFragment extends Fragment implements View.OnClickListener {

    private MainActivity activity;
    private FragmentMeBinding binding;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        activity = (MainActivity) getActivity();
        binding = DataBindingUtil.inflate(inflater,R.layout.fragment_me,container,false);
        View view = binding.getRoot();

        SQLiteDatabase database;
        database = DatabaseHelper.getDatabase(getContext(),"timetable.db");
        Cursor cursor = database.query("user",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            binding.isLogined.setText(cursor.getString(cursor.getColumnIndex("uno")));
            binding.isLoginedImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_login));
        }else {
            binding.isLogined.setText("未登录");
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        activity.getSupportActionBar().setSubtitle(null);
        binding.isLogined.setOnClickListener(this);
        binding.isLoginedImage.setOnClickListener(this);
        binding.timetableSetting.setOnClickListener(this);
        binding.softSetting.setOnClickListener(this);
        binding.tellme.setOnClickListener(this);
        binding.about.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.is_logined_image:
            case R.id.is_logined:
                startActivityForResult(new Intent(getContext(), LoginActivity.class), TagHelper.ME);
                break;
            case R.id.timetable_setting:
                startActivityForResult(new Intent(getContext(), TimetableSettingActivity.class),TagHelper.ME);
                break;
            case R.id.soft_setting:
                startActivityForResult(new Intent(getContext(), AppSettingActivity.class),TagHelper.ME);
                break;
            case R.id.tellme:
                initTellMeDialog();
                break;
            case R.id.about:
                startActivity(new Intent(getContext(), AboutActivity.class));
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==TagHelper.ME&&resultCode==TagHelper.LOGIN){
            SQLiteDatabase database;
            database = DatabaseHelper.getDatabase(getContext(),"timetable.db");
            Cursor cursor = database.query("user",null,null,null,null,null,null);
            if(cursor.moveToFirst()){
                binding.isLogined.setText(cursor.getString(cursor.getColumnIndex("uno")));
                binding.isLoginedImage.setImageDrawable(getResources().getDrawable(R.drawable.ic_login));
            }else {
                binding.isLogined.setText("未登录");
            }
            Snackbar.make(getView(),R.string.login_success,Snackbar.LENGTH_SHORT).show();
        }
        if(requestCode==TagHelper.ME&&resultCode==TagHelper.TIMETABLESETTING) {
            if(data.getIntExtra("save_success",0)==1)
                Snackbar.make(getView(),getString(R.string.save_success),Snackbar.LENGTH_SHORT).show();
        }
    }

    private void initTellMeDialog() {
        DialogInterface.OnClickListener positive = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(activity, WelcomeActivity.class));
            }
        };
        DialogInterface.OnClickListener negative = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String url="mqqwpa://im/chat?chat_type=wpa&uin=1773747161";
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
        };
        DialogFragmentHelper dialogFragmentHelper = DialogFragmentHelper.newInstance("小提示","辽大+欢迎大家反馈问题或提供意见。如果您有问题，可否先看一眼功能介绍呢~","看一看功能介绍","不看，我要反馈",true,positive,negative);
        dialogFragmentHelper.show(getFragmentManager(),"");
    }
}
